using System;
using FireStarterModeller.BO;
using Habanero.Testability;

namespace FireStarterModeller.Test
{
    public static class TestUtilsDMClass
    {
        private readonly static BOTestFactory<DMClass> _factory = GetFactory();

        public static BOTestFactory<DMClass> GetFactory()
        {
            return BOTestFactoryRegistry.Instance.Resolve<DMClass>();
        }
        /// <summary>
        /// Creates a new saved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateSavedDMClass(string className)
        {
            DMClass dMClass = CreateUnsavedValidDMClass(className);
            dMClass.Save();
            return dMClass;
        }

        /// <summary>
        /// Creates a new saved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateSavedDMClass()
        {
            DMClass dMClass = CreateUnsavedValidDMClass("A" + TestUtilsShared.GetRandomString());
            //DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dMClass);
            //dMClass.CreateObjectIdentity(dmProperty);
            dMClass.Save();
            return dMClass;
        }

        /// <summary>
        /// Creates a new unsaved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateUnsavedValidDMClass()
        {
            return CreateUnsavedValidDMClass(TestUtilsShared.GetRandomString());
        }

        /// <summary>
        /// Creates a new unsaved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateUnsavedValidDMClass(string className)
        {
/*            DMClass dMClass = new DMClass();
            dMClass.ClassNameBO = (String.IsNullOrEmpty(className)) ? TestUtilsShared.GetRandomString(20, 21) : className;
            dMClass.MappedTableName = TestUtilsShared.GetRandomString(1, 100);
            dMClass.Assembly = TestUtilsDMAssembly.CreateSavedDMAssembly();
            dMClass.DisplayName = TestUtilsShared.GetRandomString();
            dMClass.Generated = null;
//            dMClass.SuperClassID = TestUtilsDMInheritanceRelationship.CreateSavedDMInheritanceRelationship().SuperClassID;
            dMClass.MappedTableID = TestUtilsDBTable.CreateSavedDBTable().TableID;
            dMClass.ClassDescription = TestUtilsShared.GetRandomString();
            return dMClass;*/
            return CreateUnsavedValidDMClass(TestUtilsDMAssembly.CreateSavedDMAssembly(), className);
        }
        /// <summary>
        /// Creates a new unsaved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateUnsavedValidDMClass(DMAssembly assembly)
        {
            return  CreateUnsavedValidDMClass(assembly, null);
        }
        /// <summary>
        /// Creates a new unsaved DMClass with a random value assigned to every property
        /// </summary>
        public static DMClass CreateUnsavedValidDMClass(DMAssembly assembly, string className)
        {
            DMClass dMClass = new DMClass();
            dMClass.ClassNameBO = (String.IsNullOrEmpty(className)) ? TestUtilsShared.GetRandomString(20, 21) : className;
            dMClass.MappedTableName = TestUtilsShared.GetRandomString(1, 100);
            dMClass.Assembly = assembly;
            dMClass.DisplayName = TestUtilsShared.GetRandomString();
            dMClass.Generated = null;
//            dMClass.SuperClassID = TestUtilsDMInheritanceRelationship.CreateSavedDMInheritanceRelationship().SuperClassID;
  //          dMClass.MappedTableID = TestUtilsDBTable.CreateSavedDBTable().TableID;
            dMClass.ClassDescription = TestUtilsShared.GetRandomString();
            return dMClass;
        }

        /// <summary>
        /// Creates a new unsaved DMClass where all properties are null, except ID properties
        /// and those with default values.  If there are compulsory properties without
        /// defaults, saving the object will throw an exception.
        /// </summary>
        public static DMClass CreateUnsavedDefaultDMClass()
        {
            DMClass dMClass = new DMClass();
            return dMClass;
        }

        /// <summary>
        /// Creates a new unsaved DMClass where all properties are null, except ID properties
        /// and those with default values.  If there are compulsory properties without
        /// defaults, saving the object will throw an exception.
        /// </summary>
        public static DMClass CreateUnsavedDefaultDMClass(string className)
        {
            DMClass dMClass = new DMClass {ClassNameBO = className};
            return dMClass;
        }

        public static DMClass CreateUnsavedDefaultDMClass(IDMSolution solution)
        {
            return CreateUnsavedDefaultDMClass((DMSolution) solution, TestUtilsShared.GetRandomString());
        }

        public static DMClass CreateUnsavedDefaultDMClass(DMSolution solution, string className)
        {
            return CreateUnsavedDefaultDMClass(solution.DefaultAssembly, className);
        }

        public static DMClass CreateUnsavedDefaultDMClass(DMAssembly assembly, string className)
        {
            return assembly.CreateClass(className);
        }

        public static DMClass CreateUnsavedDefaultDMClass(DMAssembly assembly)
        {
            return assembly.CreateClass(TestUtilsShared.GetRandomString());
        }

        public static DMClass CreateSavedDMClass(IDMSolution solution)
        {
            DMClass dmClass = CreateUnsavedDefaultDMClass(solution);
            dmClass.Save();
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithOID(int numberOfNonIDProps)
        {
            DMClass dmClass = CreateUnsavedValidDMClass();
            dmClass.ObjectIdentity = new DMObjectIdentity();
            dmClass.ObjectIdentity.Properties.Add(TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass));
            TestUtilsDMProperty.CreateUnsavedValidDMProperties(dmClass, numberOfNonIDProps);
            dmClass.Save();
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithNoOID(int numberOfProps)
        {
            DMClass dmClass = CreateUnsavedValidDMClass();
            dmClass.ObjectIdentity = null;
            TestUtilsDMProperty.CreateUnsavedValidDMProperties(dmClass, numberOfProps);
            dmClass.Save();
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithCompositeOID(int numberOfOIDProps)
        {
            return CreateSavedDMClassWithCompositeOID(numberOfOIDProps, 0);
        }

        public static DMClass CreateSavedDMClassWithCompositeOID(int numberOfOIDProps, int numberOfNonIDProps)
        {
            DMClass dmClassWithCompositeOID = TestUtilsShared.CreateSavedDMClass();
            DMObjectIdentity objectIdentity = TestUtilsShared.CreateSavedObjectIdentityInfo(dmClassWithCompositeOID);
            TestUtilsDMProperty.CreateUnsavedValidDMProperties(dmClassWithCompositeOID, numberOfOIDProps-1);
            foreach (DMProperty property in dmClassWithCompositeOID.Properties)
            {
                objectIdentity.Properties.Add(property);
            }
            TestUtilsDMProperty.CreateUnsavedValidDMProperties(dmClassWithCompositeOID, numberOfNonIDProps);
            dmClassWithCompositeOID.Save();
            return dmClassWithCompositeOID;
        }

        public static DMClass CreateDMClassWithInheritanceRelationship()
        {
            DMClass dmClassWithInheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMClass();
            dmClassWithInheritanceRelationship.InheritanceSuperClassRelationship = new DMInheritanceRelationship();
            return dmClassWithInheritanceRelationship;
        }
        public static DMClass CreateDMClassWithObjectIdentity(IDMSolution dmSolution)
        {
            return CreateDMClassWithObjectIdentity(dmSolution, TestUtilsShared.GetRandomString());
        }

        public static DMClass CreateDMClassWithObjectIdentity(IDMSolution dmSolution, string className)
        {
            DMClass dmClass = dmSolution.DefaultAssembly.Classes.CreateBusinessObject();
            dmClass.ClassNameBO = className;
            dmClass.MappedTableName = className;
            DMProperty dmProperty = TestUtilsDMProperty.CreateDMProperty(dmClass, String.Format("{0}ID", className), "System", "Guid");
            dmClass.ObjectIdentity = new DMObjectIdentity();
            dmClass.ObjectIdentity.Properties.Add(dmProperty);
            dmClass.ObjectIdentity.IsObjectID = true;
            //UIView uiView = dmClass.UIViews.CreateBusinessObject();
            //uiView.UIGridInfo = new UIGridInfo();
            //uiView.UIFormInfo = new UIFormInfo();
            //UIGridColumnInfo uiGridColumnInfo = new UIGridColumnInfo();
            //uiGridColumnInfo.PropertyName = TestUtilsShared.GetRandomString();
            //uiGridInfo.UIColumns.Add(uiGridColumnInfo);
            return dmClass;
        }
    }
}